package com.sdp.khy.baotou.controller;


import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.khy.baotou.db.khy_package.entity.KhyPackage;
import com.sdp.khy.baotou.db.khy_package.service.IKhyPackageService;
import com.sdp.khy.baotou.db.khy_package_logistics.entity.KhyPackageLogistics;
import com.sdp.khy.baotou.utils.KhyConstants;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2023-11-29
 */
@RestController
@RequestMapping("/KhyPackageController")
public class KhyPackageController {

	@Autowired
	private IKhyPackageService iKhyPackageService;
	
	@Autowired
	private Query query;
	
	@Autowired
	private KhyPackageLogisticsController khyPackageLogisticsController;
	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(iKhyPackageService, KhyPackage.class, dataCenter));
	}
	
	@PostMapping(value = "/list")
	public Result<?> list(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.list(iKhyPackageService, KhyPackage.class, dataCenter));
	}
	
	/**
	 * 根据订单状态查询
	 * @param packageState
	 * @return
	 */
	@GetMapping(value = "/queryKhyPackageByState")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> queryKhyPackageByState(@RequestParam(name="packageState",required=true) String packageState) {
		return Result.ok(iKhyPackageService.queryKhyPackageByState(packageState));
	}
	
	
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody KhyPackage khyPackage) {
		if(khyPackage==null) {
			return Result.error("快递信息为空");
		}
		if(StringUtils.isEmpty(khyPackage.getKeyId())) {
			khyPackage.setPackageState(KhyConstants.快递状态_已下单.getCode());
			khyPackage.setOrderTime(LocalDateTime.now());
			khyPackage.setIsInvoice(KhyConstants.否.getCode());
			if(KhyConstants.付款状态_寄付现结.getCode().equals(khyPackage.getPayMode())) {
				khyPackage.setPayState(KhyConstants.是.getCode());
			}else {
				khyPackage.setPayState(KhyConstants.否.getCode());
			}
			
			iKhyPackageService.save(khyPackage);
			
			KhyPackageLogistics packageLogistics = new KhyPackageLogistics();
			packageLogistics.setLogisticsDesc("已下单");
			packageLogistics.setLogisticsTime(LocalDateTime.now());
			packageLogistics.setPackageId(khyPackage.getKeyId());
			khyPackageLogisticsController.addPackageLogistics(packageLogistics);
			
		}else {
			
			iKhyPackageService.updateById(khyPackage);
		}
		return Result.ok(khyPackage);
	}
	
	
	@GetMapping(value = "/del")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> del(@RequestParam(name="keyId",required=true) String keyId) {
		if(StringUtils.isEmpty(keyId)) {
			return Result.error("需要删除的快递信息不完整");
		}
		KhyPackage khyPackage = iKhyPackageService.queryKhyPackageById(keyId);
		if(khyPackage==null) {
			return Result.error("需要删除的快递记录不存在");
		}
		
		//如果不是已下单状态，说明此快递已经装车
		if(!KhyConstants.快递状态_已下单.getCode().equals(khyPackage.getPackageState())
				&& !KhyConstants.快递状态_已作废.getCode().equals(khyPackage.getPackageState())) {
			return Result.error("此快递已被揽收，不能删除");
		}
		iKhyPackageService.removeById(keyId);
		khyPackageLogisticsController.removePackageLogistics(keyId);
		return Result.ok();
	}
	
	@GetMapping(value = "/queryKhyPackageById")
	public Result<?> queryKhyPackageById(@RequestParam(name="keyId",required=true) String keyId) {
		if(StringUtils.isEmpty(keyId)) {
			return Result.error("快递编码不存在");
		}
		KhyPackage khyPackage = iKhyPackageService.queryKhyPackageById(keyId);
		
		return Result.ok(khyPackage);
	}
}

